package com.levi.config;

import com.levi.interceptor.RestTemplateTraceIdInterceptor;
import com.levi.threadpool.ThreadPoolExecutorMdcWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * @author shentong
 * @since 2023/1/9 15:26
 */
@Configuration
public class BeanConfig {

    @Bean
    public ThreadPoolExecutorMdcWrapper getThreadPoolExecutorMdcWrapper() {
        return new ThreadPoolExecutorMdcWrapper(5,
                10,
                5,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(5)
        );
    }

    @Bean
    public RestTemplate restTemplate( ) {
        RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
        RestTemplate restTemplate = restTemplateBuilder.build();
        //设置Resttemplate的拦截器
        restTemplate.setInterceptors(Collections.singletonList(new RestTemplateTraceIdInterceptor()));
        return restTemplate;
    }
}
